package com.robot.member.controller;

import com.alibaba.fastjson.JSON;
import com.robot.member.client.OrdersClient;
import com.robot.member.pojo.Member;
import com.robot.member.pojo.client.Orders;
import com.robot.member.service.ConsumptionService;
import com.robot.member.service.MemberService;
import com.robot.member.service.WeChatService;
import entity.Result;
import entity.StatusCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import util.CheckUtil;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @author wuyunbin
 */
@RestController
@CrossOrigin
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private ConsumptionService consumptionService;

    @Autowired
    private WeChatService weChatService;

    @Autowired
    private OrdersClient ordersClient;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private BCryptPasswordEncoder encoder;

    @Autowired
    private CheckUtil checkUtil;

    @Autowired
    private JwtUtil jwtUtil;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final String enable = "1";

    /**
     * 获取订单列表
     *
     * @param req
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/getOrdersByStatus/{page}/{size}", method = RequestMethod.POST)
    public Result getOrdersByStatus(@RequestBody HashMap<String, String> req, @PathVariable int page, @PathVariable int size) {
        Member member = memberService.findByClaims(request);
        if (member == null) {
            return new Result(StatusCode.ERROR_TOKEN_EXPIRED);
        }
        req.put("memberId", member.getId());
        logger.info("=====req=====");
        logger.info(req.toString());
        logger.info("=====req=====");
        if (enable.equals(member.getStatus())) {
            String orders = ordersClient.thirdFindOrders(req, page, size);
            return new Result(StatusCode.SUCCESS, JSON.parseObject(orders));
        }
        return new Result(StatusCode.ERROR_USER_DOES_NOTE_EXIST);
    }

    /**
     * 更新订单状态
     * @param orders
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result updateOrders(@RequestBody Orders orders) {
        ordersClient.thirdUpdateOrders(orders);
        return new Result(StatusCode.SUCCESS_UPDATED);
    }


    /**
     * 删除订单
     *
     * @param ordersId
     * @return
     */
    @RequestMapping(value = "/{ordersId}", method = RequestMethod.DELETE)
    public Result deleteOrdersById(@PathVariable String ordersId) {
        Member member = memberService.findByClaims(request);
        if (member == null) {
            return new Result(StatusCode.ERROR_TOKEN_EXPIRED);
        }
        if (enable.equals(member.getStatus())) {
            ordersClient.thirdDelete(ordersId, true);
            return new Result(StatusCode.SUCCESS_DELETED);
        }
        return new Result(StatusCode.ERROR_USER_DOES_NOTE_EXIST);
    }

    @RequestMapping(value = "/countAllStatusOrders", method = RequestMethod.GET)
    public Result countAllStatusOrders() {
        Member member = memberService.findByClaims(request);
        if (member == null) {
            return new Result(StatusCode.ERROR_USER_DOES_NOTE_EXIST);
        }
        return new Result(StatusCode.SUCCESS, ordersClient.countAllStatusOrders(member.getId()));
    }
}
